package screen
{
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class MainScreen extends Sprite
	{
		private var asset:Sprite;
		private var assetLondonBoard:MovieClip;
		
		private var jsonArray:Array;
		private var currentPage:int = 1;
		
		public function MainScreen( asset:Sprite )
		{
			this.asset = asset;	
			
			var loader:Loader = new Loader();
			this.addChild( loader );
			var mRequest:URLRequest = new URLRequest("Board.swf");
			
			loader.load( mRequest );
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
			
			
		}
		
		public function onLoaded( event:Event ):void
		{
			var ClassDefinition:Class = event.target.applicationDomain.getDefinition("LondonBoard") as Class;
			this.assetLondonBoard = new ClassDefinition();
			
			var loader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest();
			request.url = "https://london-2012-medalboard.googlecode.com/svn/trunk/BoardInfo.json";
			loader.addEventListener(Event.COMPLETE, onJSONLoaded);
			loader.load(request);
		}
		
		public function onJSONLoaded( event:Event ):void
		{
			this.asset.addChild( this.assetLondonBoard );
			
			var loader:URLLoader = URLLoader(event.target);
			this.jsonArray = (JSON.parse(loader.data)) as Array;

			this.sortJSON();
			this.writePage( this.currentPage );
			
			this.controlArrowsVisibility();

			this.assetLondonBoard.ArrowRight.addEventListener( MouseEvent.CLICK, showRightPage );
			this.assetLondonBoard.ArrowLeft.addEventListener( MouseEvent.CLICK, showLeftPage );
		}
		
		public function controlArrowsVisibility():void
		{
			if( this.currentPage - 1 >= 1 )
			{
				this.assetLondonBoard.ArrowLeftDisabled.visible = false;
			} else {
				this.assetLondonBoard.ArrowLeftDisabled.visible = true;
			}
			
			var totalPages:int = this.jsonArray.length/10
			if( this.currentPage + 1 < totalPages )
			{
				this.assetLondonBoard.ArrowRightDisabled.visible = false;
			} else {
				this.assetLondonBoard.ArrowRightDisabled.visible = true;
			}
			
		}
		
		public function showLeftPage( e:Event ):void
		{
			if( this.currentPage - 1 >= 1 )
			{
				this.currentPage--;
				this.writePage( this.currentPage );
			}
			
			this.controlArrowsVisibility();
		}
		
		public function showRightPage( e:Event ):void
		{
			var totalPages:int = this.jsonArray.length/10
			if( this.currentPage + 1 < totalPages )
			{
				this.currentPage++;
				this.writePage( this.currentPage );
			}
			
			this.controlArrowsVisibility();
		}
		
		
		public function sortJSON():void
		{
			//insertion sort
			for(var i:int = 1; i < this.jsonArray.length;i++)
			{
				var value:Object = this.jsonArray[i];
				var j:int = i-1;
				while(j >= 0 && int(this.jsonArray[j].gold) < int(value.gold))
				{
					this.jsonArray[j+1] = this.jsonArray[j];
					j--;
				}
				this.jsonArray[j+1] = value;
			}
		}
		
		
		public function writePage( page:int ):void
		{
			var intervalSets:Object = {startSetIndex : (page*11) - 11, endSetIndex : page*11};
			
			var row:int = 1;

			for( var index:int = intervalSets.startSetIndex; index <= intervalSets.endSetIndex ; index++ )
			{
				var myLoader:Loader = new Loader();
				
				var fileRequest:URLRequest = new URLRequest(this.jsonArray[index].flag);
				myLoader.load(fileRequest);
				this.assetLondonBoard[ "FlagSlot" + row ].removeChildren();
				this.assetLondonBoard[ "FlagSlot" + row ].addChild( myLoader );

				this.assetLondonBoard[ "lblCountry" + row ].text = index + 1 + "º " + this.jsonArray[index].country;
				this.assetLondonBoard[ "lblGold" + row ].text = this.jsonArray[index].gold;
				this.assetLondonBoard[ "lblSilver" + row ].text = this.jsonArray[index].silver;
				this.assetLondonBoard[ "lblBronze" + row ].text = this.jsonArray[index].bronze;
				
				row++;
				if( row > 12)
					break;
			}
		}
	}

}